// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

//引入自定义组件
import plugin from './components/index'

import {
  localData
} from './assets/js/local'

// 引入element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

//引入axios请求
import axios from 'axios';
import Qs from 'qs';


import {
  filterTime
} from './assets/js/time'

//跨域带上cookie
axios.defaults.withCredentials = true;

Vue.use(ElementUI);
Vue.use(plugin);


Vue.config.productionTip = false

//localStorage操作
Vue.prototype.localData = localData



// 短信云后台管理系统--测试
Vue.prototype.host = 'http://192.168.3.55:40003'

//短信云后台管理系统--正式
// Vue.prototype.host = 'http://47.99.170.92:40002'


//封装ajax -- post
Vue.prototype.ajaxJson = function (options) {
  let that = this;
  if (options.data) {
    if (options.data.hasOwnProperty("account") && options.data.hasOwnProperty("password")) {
      options.data.role = '';
    } else {
      let role = localData('get', 'login_user').role;
      options.data.role = role
      let account = localData('get', 'login_user').account
      if(role == '1'){
        options.data.sendAccount = account
      }
    }
  }
  axios({
    url: that.host + options.url,
    method: "POST",
    headers: options.headers,
    data: options.data,
  }).then(function (res) {
    if (res.status == 200) {
      options.success(res.data);
      let code = res.data.code;
      if (code != '0') {
        that.$message.error(res.data.message)
        if (code == '1003') {
          that.logout()
        }
      }
    }
  }).catch(function (error) {
    if (error.response) {
      let code = error.response.status;
      if (code == '404') {
        that.$message.error('接口不存在');
        options.success({})
      } else {
        that.$message.error('服务器错误');
        options.success({})
      }
    }
  })
}


//封装ajax -- get
Vue.prototype.ajaxJsonGet = function (options) {
  let that = this;
  if (options.data) {
    options.data.role = localData('get', 'login_user').role
  }
  axios({
    url: that.host + options.url,
    method: "GET",
    headers: options.headers,
    data: options.data,
  }).then(function (res) {
    if (res.status == 200) {
      options.success(res.data);
      let code = res.data.code;
      if (code != '0') {
        that.$message.error(res.data.message)
        if (code == '1003') {
          that.logout()
        }
      }
    }
  }).catch(function (error) {
    if (error.response) {
      let code = error.response.status;
      if (code == '404') {
        that.$message.error('接口不存在');
        options.success({})
      } else {
        that.$message.error('服务器错误');
        options.success({})
      }
    }
  })
}
//封装请求-formdata
Vue.prototype.ajax = function (options) {
  let that = this;
  let data = Qs.stringify(options.data);
  axios({
    url: that.host + options.url,
    method: 'POST',
    headers: options.headers,
    data: data,
  }).then(function (res) {
    if (res.status == 200) {
      options.success(res.data);
      let code = res.data.code;
      if (code != '0') {
        that.$message.error(res.data.message);
        if (code == '1003') {
          that.logout()
        }
      }
    }
  }).catch(function (error) {
    if (error.response) {
      let code = error.response.status;
      if (code == '404') {
        that.$message.error('接口不存在');
        options.success({})
      } else {
        that.$message.error('服务器错误');
        options.success({})
      }
    }
  })
}


//错误提示
Vue.prototype.showMsg = function (val, status) {
  let that = this;
  that.$message({
    message: val,
    type: status
  })
}


//退出登录
Vue.prototype.logout = function () {
  let that = this;
  that.ajaxJson({
    url: '/customerManage/auth/logout',
    success: function (res) {
      that.localData('clean', 'login_user');
      that.$router.push({
        name: 'login'
      })
    }
  })
}

//表格显示时间
Vue.prototype.formatter_time = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd') : '--'
}
Vue.prototype.formatter_timeYMDHMS = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd hh:mm:ss') : '--'
}
Vue.prototype.formatter_timeYMDHM = function (row, column, cellValue, index) {
  return cellValue ? (new Date(cellValue)).Format('yyyy-MM-dd hh:mm') : '--'
}


//其他位置时间显示
Vue.filter('filterTime', filterTime)
//时间选择器限制当前时间
Vue.prototype.pickerOptions = {
  disabledDate(time) {
    return time.getTime() > Date.now();
  }
}
//时间Format
Date.prototype.Format = function (fmt) {
  let o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)))
    }
  }
  return fmt;
}


Vue.prototype.sendStatus = function (row, column, cellValue, index) {
  switch (cellValue) {
    case "0":
    case 0:
      return "发送中";
    case 1:
      return "发送中";
    case 2:
      return "发送成功";
    case 3:
      return "发送失败";
  }
}


// oss参数测试
Vue.prototype.OssParameter = {
  accessKeyId: 'LTAI83HVuUfaB4UI',
  accessKeySecret: 'Z7ucFCs4Z4oTjuHrEbbWex1Jj8yyrY',
  region: 'oss-cn-hangzhou',
  bucket: 'guoyuechuzu',
  folder: 'product_picture/'
};

Vue.prototype.uploadFile = function (options) {
  let that = this;
  if (!options.file) {
    return;
  }
  let fileName = options.file.name;
  let filelastName = fileName.substring(fileName.lastIndexOf(".") + 1)
  let ossParameter = that.OssParameter;
  let client = new OSS.Wrapper({
    secure: true,
    accessKeyId: ossParameter.accessKeyId,
    accessKeySecret: ossParameter.accessKeySecret,
    region: ossParameter.region,
    bucket: ossParameter.bucket,
  });
  let path = ossParameter.folder + (new Date()).getTime() + parseInt((Math.random() * 100000 + 0), 10) + '.' + filelastName;
  client.multipartUpload(path, options.file).then(function (res) {
    let href = 'https://' + ossParameter.bucket + '.' + ossParameter.region + '.aliyuncs.com/' + res.name;
    options.success(href)
  }).catch(function (err) {});
}



/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: {
    App
  },
  template: '<App/>'
})
